package com.scpii.universal.pns.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.entity.ContentImgTab;
import com.scpii.universal.pns.dto.ActivitiesJoinDto;
import com.scpii.universal.pns.dto.UsersActionDto;
import com.scpii.universal.pns.entity.Comments;
import com.scpii.universal.pns.entity.Resources;
import com.scpii.universal.pns.service.ActivitiesService;
import com.scpii.universal.pns.service.CircleService;
import com.scpii.universal.pns.service.ResourcesService;
import com.scpii.universal.service.ContentImgService;

@Controller
@Scope("prototype")
public class ActivitiesMessageAction extends BaseAction {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 640376469611095221L;
	@Resource
	private ActivitiesService activitiesService;
	@Resource
	private ContentImgService contentImgService;
	@Resource
	private CircleService circleService;
	@Resource
	private ResourcesService resourcesService;
	private Pagination page = new Pagination();
	private Pagination pageSummary = new Pagination();
	private Pagination pageUserAction = new Pagination();
	private Pagination pageComment = new Pagination();
	private Pagination pagePhoto = new Pagination();
	private List<ActivitiesJoinDto> actiList;

	private String activitiesId;
	private String actionType;
	private String tarId;
	private String commentsId;
	private List<Comments> commList;;
	private List<UsersActionDto> userActionList;
	private List<Resources> resourcesinfo;
	List<ContentImgTab> conImgList;
	private String intPhotoId;
	private Integer typeFlag;
	private String intContentId;
	private Log log = LogFactory.getLog(this.getClass());

	/**
	 * 页面初始化加载
	 * 
	 * @param request
	 * @param response
	 */
	public String getActivitiesInit() {
		// 判断第一次进入分页信息
		if (getSelectPerPage("page") == -1) {
			page.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			page.setPageRecord(getSelectPerPage("page"));
		}

		// 判断是否是第一次查询

		if (getQueryFlag("page") == -1) {
			page.setTotalRecord(activitiesService
					.getActivitidesInitCount(getUserInfo().getAppId()));
		} else {
			page.setTotalRecord(getQueryFlag("page"));
		}
		setPageParameters(page, "page");

		actiList = activitiesService.getActivitidesInit(getUserInfo()
				.getAppId(), page.getCurrentPage(), page.getPageRecord());

		return SUCCESS;

	}

	/**
	 * 获取活动的图片getImgListById
	 * 
	 * @return
	 */
	public String getActiPhoto() {
		conImgList = contentImgService.getImgListById(Integer
				.parseInt(intContentId));
		return SUCCESS;
	}

	// /------------用户动作管理--------------------
	public String getJoinCount() {
		log.info("用户动作管理");
		// 获取以前的messge信息

		if (getSelectPerPage("pageUserAction") == -1) {
			pageUserAction.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			pageUserAction.setPageRecord(getSelectPerPage("pageUserAction"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("pageUserAction") == -1) {
			pageUserAction.setTotalRecord(activitiesService.getUserActionCount(
					Long.parseLong(activitiesId), actionType));
		} else {
			pageUserAction.setTotalRecord(getQueryFlag("pageUserAction"));
		}
		setPageParameters(pageUserAction, "pageUserAction");
		userActionList = activitiesService
				.getUserAction(Long.parseLong(activitiesId), actionType,
						pageUserAction.getCurrentPage(),
						pageUserAction.getPageRecord());
		return SUCCESS;

	}

	/**
	 * 获取活动评论
	 * 
	 * @return
	 */
	public String getCommentInit() {
		// 获取以前的messge信息
		typeFlag = 0;
		if (getSelectPerPage("pageSummary") == -1) {
			pageSummary.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			pageSummary.setPageRecord(getSelectPerPage("pageSummary"));
		}
		if (!tarId.startsWith("48")) {
			tarId = getNewTarId(tarId);
		}
		// 判断是否是第一次查询
		if (getQueryFlag("page") == -1) {
			pageSummary.setTotalRecord(circleService.getCircleInitCount(
					getUserInfo().getAppId(), Long.parseLong(tarId)));
		} else {
			pageSummary.setTotalRecord(getQueryFlag("pageSummary"));
		}
		setPageParameters(pageSummary, "pageSummary");
		commList = circleService.getCircleInit(getUserInfo().getAppId(),
				Long.parseLong(tarId), pageSummary.getCurrentPage(),
				pageSummary.getPageRecord());
		return SUCCESS;

	}

	/**
	 * 获取活动评论回复
	 * 
	 * @return
	 */
	public String getCommentBackInit() {
		typeFlag = 1;
		if (getSelectPerPage("pageComment") == -1) {
			pageComment.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			pageComment.setPageRecord(getSelectPerPage("pageComment"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("pageComment") == -1) {
			pageComment.setTotalRecord(circleService
					.getCircleInitByResIdCount(Long.parseLong(commentsId)));
		} else {
			pageComment.setTotalRecord(getQueryFlag("pageComment"));
		}
		setPageParameters(pageComment, "pageComment");
		commList = circleService.getCircleByResIdInit(
				Long.parseLong(commentsId), pageComment.getCurrentPage(),
				pageComment.getPageRecord());
		return SUCCESS;
	}

	/**
	 * 根据资源id 删除回复及回复的子类
	 * 
	 * @param resId
	 */
	public String deleteByResId() {
		circleService.updateByResId(Long.parseLong(commentsId));
		try {
			response.getWriter().print(1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 获取评论的图片
	 * 
	 * @return
	 */
	public String getCommentPhoto() {
		if (getSelectPerPage("pagePhoto") == -1) {
			pagePhoto.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			pagePhoto.setPageRecord(getSelectPerPage("pagePhoto"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("pagePhoto") == -1) {
			pagePhoto.setTotalRecord(resourcesService.getPhotoInfoCount(Long
					.parseLong(commentsId)));
		} else {
			pagePhoto.setTotalRecord(getQueryFlag("pagePhoto"));
		}
		setPageParameters(pagePhoto, "pagePhoto");
		resourcesinfo = resourcesService.getPhotoInfo(
				Long.parseLong(commentsId), pagePhoto.getCurrentPage(),
				pagePhoto.getPageRecord());

		return SUCCESS;
	}

	public String deleteByPhotoId() {

		try {
			resourcesService.updateResById(Long.parseLong(intPhotoId));
			response.getWriter().print(1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}

	private String getNewTarId(String tarId) {
		// 110000141146
		int length = tarId.toString().length();
		length = 10 - length;
		StringBuffer temp = new StringBuffer("48");
		for (int i = 0; i < length; i++) {
			temp.append("0");
		}
		temp.append(tarId);
		return temp.toString();
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public List<ActivitiesJoinDto> getActiList() {
		return actiList;
	}

	public void setActiList(List<ActivitiesJoinDto> actiList) {
		this.actiList = actiList;
	}

	public Pagination getPageUserAction() {
		return pageUserAction;
	}

	public void setPageUserAction(Pagination pageUserAction) {
		this.pageUserAction = pageUserAction;
	}

	public String getActivitiesId() {
		return activitiesId;
	}

	public void setActivitiesId(String activitiesId) {
		if (activitiesId != null && !"".equals(activitiesId)) {
			String[] names = activitiesId.split(",");
			if (names != null && names.length > 0) {
				activitiesId = names[0];
			}
		}
		this.activitiesId = activitiesId;
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		if (actionType != null && !"".equals(actionType)) {
			String[] names = actionType.split(",");
			if (names != null && names.length > 0) {
				actionType = names[0];
			}
		}
		this.actionType = actionType;
	}

	public List<UsersActionDto> getUserActionList() {
		return userActionList;
	}

	public void setUserActionList(List<UsersActionDto> userActionList) {
		this.userActionList = userActionList;
	}

	public Pagination getPageComment() {
		return pageComment;
	}

	public void setPageComment(Pagination pageComment) {
		this.pageComment = pageComment;
	}

	public Pagination getPagePhoto() {
		return pagePhoto;
	}

	public void setPagePhoto(Pagination pagePhoto) {
		this.pagePhoto = pagePhoto;
	}

	public Pagination getPageSummary() {
		return pageSummary;
	}

	public void setPageSummary(Pagination pageSummary) {
		this.pageSummary = pageSummary;
	}

	public String getTarId() {
		return tarId;
	}

	public void setTarId(String tarId) {
		if (tarId != null && !"".equals(tarId)) {
			String[] names = tarId.split(",");
			if (names != null && names.length > 0) {
				tarId = names[0];
			}
		}
		this.tarId = tarId;
	}

	public Integer getTypeFlag() {
		return typeFlag;
	}

	public void setTypeFlag(Integer typeFlag) {
		this.typeFlag = typeFlag;
	}

	public String getCommentsId() {
		return commentsId;
	}

	public void setCommentsId(String commentsId) {
		if (commentsId != null && !"".equals(commentsId)) {
			String[] names = commentsId.split(",");
			if (names != null && names.length > 0) {
				commentsId = names[0];
			}
		}
		this.commentsId = commentsId;
	}

	public List<Comments> getCommList() {
		return commList;
	}

	public void setCommList(List<Comments> commList) {
		this.commList = commList;
	}

	public List<Resources> getResourcesinfo() {
		return resourcesinfo;
	}

	public void setResourcesinfo(List<Resources> resourcesinfo) {
		this.resourcesinfo = resourcesinfo;
	}

	public String getIntPhotoId() {
		return intPhotoId;
	}

	public void setIntPhotoId(String intPhotoId) {
		this.intPhotoId = intPhotoId;
	}

	public List<ContentImgTab> getConImgList() {
		return conImgList;
	}

	public void setConImgList(List<ContentImgTab> conImgList) {
		this.conImgList = conImgList;
	}

	public String getIntContentId() {
		return intContentId;
	}

	public void setIntContentId(String intContentId) {
		this.intContentId = intContentId;
	}

}
